﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using FolderSyncLib;

namespace FolderSyncService
{
    public partial class FolderSyncService : ServiceBase
    {
        private FolderSync _folderSync;
        public FolderSyncService()
        {
            InitializeComponent();
            try
            {
                string eventSource = "Sync_Source";
                string evenLog = "Sync_Log";

                if (!(System.Diagnostics.EventLog.SourceExists(eventSource)))
                {
                    System.Diagnostics.EventLog.CreateEventSource(eventSource, evenLog);
                }
                eventLog1.Source = eventSource;
                eventLog1.Log = evenLog;

                
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error {0}", ex.Message));
            }
        }

        public void WriteLog(string content, EventLogEntryType type)
        {
            eventLog1.WriteEntry(content, type);
        }

        //private System.Timers.Timer timer1;
        /// <summary>
        /// Khai báo hàm này mục đích dùng cho trường hợp debug
        /// </summary>
        public void StartSync()
        {
            try
            {
                //Enable đồng bộ thư mục theo ngày hiện tại
                //timer1 = new Timer(10000);
                //timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                //timer1.Enabled = true;
                string folderSync = ConfigurationManager.AppSettings["foldersync"];
                string ftpserver = ConfigurationManager.AppSettings["ftpserver"];
                string username = ConfigurationManager.AppSettings["username"];
                string password = Helper.DeCrypt(ConfigurationManager.AppSettings["password"], "12345");
                _folderSync = new FolderSync(folderSync, ftpserver, username, password, eventLog1);
                //Bắt đầu giám sát thư mục khi có thay đổi
                _folderSync.StartFileSystemWatcher();
                WriteLog("Start Service", EventLogEntryType.Information);

            }
            catch (Exception ex)
            {
                WriteLog(string.Format("Error Start Service: {0}", ex.Message), EventLogEntryType.Error);
            }
        }

        protected override void OnStart(string[] args)
        {
            StartSync();
        }

        protected override void OnStop()
        {
            try
            {
                //timer1.Enabled = false;
                WriteLog("Stop Service", EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                WriteLog(string.Format("Error Stop Service: {0}", ex.Message),EventLogEntryType.Error);
            }
        }

        
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            //try
            //{
            //    if (_folderSync._fileList.Count == 0)
            //    {
            //        //Sau một khoảng thời gian là bắt đầu kiểm tra đồng bộ
            //        WriteLog("Start Sync Folder", EventLogEntryType.Information);
            //        _folderSync.StartSyncFolder();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    WriteLog(string.Format("Error Stop Service: {0}", ex.Message), EventLogEntryType.Error);
            //}
        }

        
    }
}
